﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.Admin.Master;


namespace Besture.UI.Admin.CommonModel
{
    public partial class ModelSet : BasePage
    {

        public string Table = "TB";

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);

            ((ConfigAdmin)this.Master).Title = "内容模型管理";
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            //添加页面
            if (id <= 0)
            {
                txtTableName.Text = Table;
                return;
            }
            ContentModelDac dac = new ContentModelDac();
            ContentModelModel model = dac.GetModel(id);

            txtName.Text = model.Name;
            txtProjectRemark.Text = model.ProjectRemark;
            txtTableName.Text = model.TableName;
            txtContentTemplate.Text = model.ContentTemplate;
            txtTableName.Enabled = false;
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            ContentModelDac dac = new ContentModelDac();
            ContentModelModel model = null;
            //添加
            if (!EditPage)
            {
                model = new ContentModelModel();
                model.Name = txtName.Text;
                model.TableName = txtTableName.Text;
                model.ContentTemplate = txtContentTemplate.Text;
                model.ProjectRemark = txtProjectRemark.Text;

                //判断数据库是否存在相同的内容模型名称
                if (dac.HasExistModelName(model.Name))
                {
                    PageAlert("内容模型名称已存在，请更改");
                    return;
                }
                //验证数据库表名是否按规则输入 (TB前缀)
                if (model.TableName.IndexOf(Table)!=0)
                {
                    PageAlert("数据库表名请以“TB”开头，请更改");
                    return;
                }

                //判断数据库是否存在相同的数据库表名
                if (dac.HasExistModelTable(model.TableName))
                {
                    PageAlert("数据库表名已存在，请更改");
                    return;
                }

                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.Name = txtName.Text;
                model.TableName = txtTableName.Text;
                model.ContentTemplate = txtContentTemplate.Text;
                model.ProjectRemark = txtProjectRemark.Text;
                dac.Update(model);
            }
            FunHelper.Alert("保存成功", "ModelList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("ModelList.aspx");
        }

        #endregion
    }
}